/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gestors;

import com.gr.uth.dao.UserDAO;
import com.gr.uth.dao.controllers.UserDAOJpaController;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.transaction.NotSupportedException;
import javax.transaction.SystemException;
import javax.transaction.UserTransaction;

/**
 *
 * @author mruiz
 */
public class RegistrationService {
    
    private static final String PERSISTENCE_UNIT_NAME = "startupme";
    private static EntityManagerFactory factory;
    
    public static String signUpUser(String email, String password, String username, String speciality, String birth_date, String country, String gravatarID, String googleCodeID, String linkedInID, String facebookID, String twitterID) {
        String operationResult = "";
        if (!email.isEmpty() && !password.isEmpty()) {
            
            factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
            UserDAOJpaController uc = new UserDAOJpaController(null, factory);
            try {
                password = encodeInMD5(password);
                UserDAO user = new UserDAO(email, password, 0, 0, username);
                if (!birth_date.isEmpty() && birth_date != null) {
                    user.setBirthDay(new Date(birth_date));
                } else {
                    user.setBirthDay(null);
                }
                user.setFacebookURL(facebookID);
                user.setGoogleCodeID(googleCodeID);
                user.setGravatarID(gravatarID);
                user.setLinkedLinkID(linkedInID);
                user.setNationality(country);
                user.setOccupation(speciality);
                uc.create(user);
                operationResult = "Account successfully created";
            } catch (NoSuchAlgorithmException ex) {
                Logger.getLogger(RegistrationService.class.getName()).log(Level.SEVERE, null, ex);
                operationResult = ex.getMessage();
            } catch (UnsupportedEncodingException ex) {
                operationResult = ex.getMessage();
                Logger.getLogger(RegistrationService.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return operationResult;
    }
    
    private static String encodeInMD5(String password) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        
        MessageDigest m = MessageDigest.getInstance("MD5");
        m.update(password.getBytes("UTF8"));
        byte s[] = m.digest();
        String result = "";
        for (int i = 0; i < s.length; i++) {
            result += Integer.toHexString((0x000000ff & s[i]) | 0xffffff00).substring(6);
        }
        return result;
    }
}
